﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace shenfenzhengjianyan
{
    internal class Program
    {
        static void Main(string[] args)
        {
            
            Regex r = new Regex(@"^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$");
            string c = "";
            while (c != "\n")
            {
                c=Console.ReadLine();
                if (r.IsMatch(c) && jiaoyanLast(c))
                {
                    Console.WriteLine("is Legal!");
                }
                else
                {
                    Console.WriteLine("is Illegal!");
                }
            }
        }

        static bool jiaoyanLast(string c)
        {
            int[] weightTable = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            int[] m = new int[17];
            int last=-1;
            int count = 0;
            foreach(char a in c)
            {
                if (count >= 17) {string b = a.ToString();
                    if (b == "X" || b == "x") { last = 10; } else { last = int.Parse(b); } break; }
                m[count] = int.Parse(a.ToString());
                count += 1;
                
            }
            
            
            int sum = 0;
            for(int i = 0; i < 17; i++)
            {
                sum += m[i] * weightTable[i];
            }
            if ((12-(sum % 11))%11 == last) { return true; }
            else { return false; }
        }
    }
}
